#include "log4cpp/Category.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/RollingFileAppender.hh" //回卷文件
#include "log4cpp/PatternLayout.hh" //设置日志布局的头文件
#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::string;
using namespace log4cpp;

int main(void)
{
    //设计日志布局
    PatternLayout *pt1 = new PatternLayout();
    pt1->setConversionPattern("%d %c [%p] %m %n");

    //设计布局（普通文件的）
    PatternLayout *pt2 = new PatternLayout();
    pt2->setConversionPattern("%d %c [%p] %m %n");

    //设计布局（回滚文件的)
    PatternLayout *pt3 = new PatternLayout();
    pt3->setConversionPattern("%d %c [%p] %m %n");

    //创建目的地对象，绑定布局
    OstreamAppender *pos = new OstreamAppender("consle",&cout);//绑定cout //输出到终端
    pos->setLayout(pt1);

    //创建目的地对象，输出到file
    FileAppender *fileAPP = new FileAppender("default","file1.txt");
    fileAPP->setLayout(pt2);
    
    //创建目的地对象，输出到回滚文件 //设置单个文件大小是5k
    RollingFileAppender *rollFileAPP = new RollingFileAppender("Rolling","RollFile.log",5*1024,9);
    rollFileAPP->setLayout(pt3);

    //创建日志记录(模块)
    Category &root = Category::getRoot(); //设置根模块
    //叶子模块
    Category &saleDepart = Category::getInstance("saleDepart"); //取名sub1 这个主要和配置文件绑定

    //叶子模块2
    Category &fileDepart = Category::getInstance("fileDepart");

    //叶子模块3
    Category &rollfileDepart = Category::getInstance("rollfileDepart");

    //设置配置模块优先级
    root.setPriority(Priority::WARN);
    //saleDepart.setPriority(Priority::ERROR);
    fileDepart.setPriority(Priority::INFO);
    rollfileDepart.setPriority(Priority::INFO);


    //添加目的地址对象
    saleDepart.addAppender(pos);
    saleDepart.warn("1 + 1 == two ?");
    saleDepart.error("this is error");
    saleDepart.debug("this is debug");
    //普通文件对象
    fileDepart.addAppender(fileAPP);
    fileDepart.info("this is file info");
    fileDepart.debug("this is file debug");
    fileDepart.emerg("this is file emerg");
    fileDepart.error("this is file error");
    fileDepart.warn("this is file warn");
    //回滚文件对象
    rollfileDepart.addAppender(rollFileAPP);
    int i = 100;
    while(i-->0){
        rollfileDepart.info("this is file info");
        rollfileDepart.debug("this is file debug");
        rollfileDepart.emerg("this is file emerg");
        rollfileDepart.error("this is file error");
        rollfileDepart.warn("this is file warn");
        rollfileDepart.info("this is file info");
        rollfileDepart.debug("this is file debug");
        rollfileDepart.emerg("this is file emerg");
        rollfileDepart.error("this is file error");
        rollfileDepart.warn("this is file warn");
        rollfileDepart.info("this is file info");
        rollfileDepart.debug("this is file debug");
        rollfileDepart.emerg("this is file emerg");
        rollfileDepart.error("this is file error");
        rollfileDepart.warn("this is file warn");
        rollfileDepart.info("this is file info");
        rollfileDepart.debug("this is file debug");
        rollfileDepart.emerg("this is file emerg");
        rollfileDepart.error("this is file error");
        rollfileDepart.warn("this is file warn");
    }
    return 0;
}